/**
 * Copyright 2010 upTick Pty Ltd
 *
 * Licensed under the terms of the GNU General Public License version 3 
 * as published by the Free Software Foundation. You may obtain a copy of the
 * License at: http://www.gnu.org/copyleft/gpl.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations 
 * under the License.
 */

package au.com.uptick.serendipity.client.sales.account.view;

import au.com.uptick.serendipity.client.Serendipity;
import au.com.uptick.serendipity.client.sales.account.presenter.AccountInformationPresenter;
import au.com.uptick.serendipity.shared.dto.AccountDto;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewImpl;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.*;
import com.smartgwt.client.widgets.form.fields.events.FormItemClickHandler;
import com.smartgwt.client.widgets.form.fields.events.FormItemIconClickEvent;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;

import java.util.LinkedHashMap;

public class AccountInformationView extends ViewImpl implements AccountInformationPresenter.MyView {

    private static final String GENERAL_INFORMATION_SECTION_DISPLAY_NAME = "General Information";
    private static final String ACCOUNT_NAME = "accountName";
    private static final String ACCOUNT_NAME_DISPLAY_NAME = "Account Name";
    private static final String ACCOUNT_NUMBER = "accountNumber";
    private static final String ACCOUNT_NUMBER_DISPLAY_NAME = "Account Number";
    private static final String PARENT_ACCOUNT = "parentAccount";
    private static final String PARENT_ACCOUNT_DISPLAY_NAME = "Parent Account";
    private static final String PRIMARY_CONTACT = "primaryContact";
    private static final String PRIMARY_CONTACT_DISPLAY_NAME = "Primary Contact";
    private static final String RELATIONSHIP_TYPE = "relationshipType";
    private static final String RELATIONSHIP_TYPE_DISPLAY_NAME = "Relationship Type";
    private static final String MAIN_PHONE = "mainPhone";
    private static final String MAIN_PHONE_DISPLAY_NAME = "Main Phone";
    private static final String OTHER_PHONE = "otherPhone";
    private static final String OTHER_PHONE_DISPLAY_NAME = "Other Phone";
    private static final String FAX = "fax";
    private static final String FAX_DISPLAY_NAME = "Fax";
    private static final String WEB_SITE = "webSite";
    private static final String WEB_SITE_DISPLAY_NAME = "Web Site";
    private static final String EMAIL = "email";
    private static final String EMAIL_DISPLAY_NAME = "Email";

    private static final String ADDRESS_INFORMATION_SECTION_DISPLAY_NAME = "Address Information";

    private AccountDto accountDto;

    private Long accountId;
    private TextItem accountName;
    private TextItem accountNumber;

    private TabSet tabSet;
    private Tab generalTab;
    // private Tab administrationTab;
    // private Tab notesTab;

    private DynamicForm generalForm;
    // private DynamicForm administrationForm;

    private VLayout panel;

    @Inject
    public AccountInformationView() {

        Log.debug("AccountInformationView()");

        accountDto = null;

        panel = new VLayout();

        // initialise the Information View layout container
        panel.setStyleName("crm-Entity-ContextArea");
        panel.setWidth("*");

        tabSet = new TabSet();

        initGeneralTab();
        // initAdministrationTab();
        // initNotesTab();

        tabSet.setTabs(generalTab);

        panel.addMember(tabSet);
    }

    private void initGeneralTab() {
        generalTab = new Tab(Serendipity.getConstants().GeneralTab());
        generalTab.setWidth(70);

        generalForm = new DynamicForm();
        generalForm.setMargin(2);
        generalForm.setNumCols(4);
        generalForm.setCellPadding(2);
        generalForm.setAutoFocus(false);
        generalForm.setWrapItemTitles(false);

        // no ":" after the field name
        generalForm.setTitleSuffix(" ");
        generalForm.setRequiredTitleSuffix(" ");

        // generalForm.setDataSource(supplyItemDS);
        // generalForm.setUseAllDataSourceFields(true);

        // Section 1 Column 1
        accountName = new TextItem(ACCOUNT_NAME, ACCOUNT_NAME_DISPLAY_NAME);
        accountName.setSelectOnFocus(true);
        // accountName.setWrapTitle(false);
        // accountName.setDefaultValue("[Account Name]");
        accountName.setRequired(true);

        accountNumber = new TextItem(ACCOUNT_NUMBER, ACCOUNT_NUMBER_DISPLAY_NAME);

        PickerIcon searchParentAccount = new PickerIcon(PickerIcon.SEARCH, new FormItemClickHandler() {
            public void onFormItemClick(FormItemIconClickEvent event) {
                SC.say("Parent Account clicked!");
            }
        });
        TextItem parentAccount = new TextItem(PARENT_ACCOUNT, PARENT_ACCOUNT_DISPLAY_NAME);
        parentAccount.setIcons(searchParentAccount);

        PickerIcon searchPrimaryContact = new PickerIcon(PickerIcon.SEARCH, new FormItemClickHandler() {
            public void onFormItemClick(FormItemIconClickEvent event) {
                SC.say("Primary Contact clicked!");
            }
        });
        TextItem primaryContact = new TextItem(PRIMARY_CONTACT, PRIMARY_CONTACT_DISPLAY_NAME);
        primaryContact.setIcons(searchPrimaryContact);

        ComboBoxItem relationshipType = new ComboBoxItem(RELATIONSHIP_TYPE, RELATIONSHIP_TYPE_DISPLAY_NAME);
        relationshipType.setType("comboBox");
        relationshipType.setValueMap("Active", "Inactive");
        relationshipType.setDefaultToFirstOption(true);

        // Section 1 Column 2
        TextItem mainPhone = new TextItem(MAIN_PHONE, MAIN_PHONE_DISPLAY_NAME);
        TextItem otherPhone = new TextItem(OTHER_PHONE, OTHER_PHONE_DISPLAY_NAME);
        TextItem fax = new TextItem(FAX, FAX_DISPLAY_NAME);
        TextItem webSite = new TextItem(WEB_SITE, WEB_SITE_DISPLAY_NAME);
        TextItem email = new TextItem(EMAIL, EMAIL_DISPLAY_NAME);

        SectionItem generalSection = new SectionItem();
        generalSection.setDefaultValue(GENERAL_INFORMATION_SECTION_DISPLAY_NAME);
        generalSection.setItemIds(ACCOUNT_NAME, MAIN_PHONE,
                                  ACCOUNT_NUMBER, OTHER_PHONE,
                                  PARENT_ACCOUNT, FAX,
                                  PRIMARY_CONTACT, WEB_SITE,
                                  RELATIONSHIP_TYPE, EMAIL);

        // Section 2 Column 1
        TextItem addressName = new TextItem("addressName", "Address Name");

        TextItem postCode = new TextItem("postCode", "ZIP/Postal Code");

        TextItem street1 = new TextItem("street1", "Street 1");

        // TO DO - find out why the flag icons arn't working ???
        SelectItem country = new SelectItem();
        country.setName("country");
        country.setTitle("Country");
        LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>();
        valueMap.put("au", "Australia");
        valueMap.put("nz", "New Zealand");
        country.setValueMap(valueMap);
        country.setDefaultToFirstOption(true);
        country.setImageURLPrefix("flags/16/");
        country.setImageURLSuffix(".png");
        country.setIconHeight(16);

        TextItem street2 = new TextItem("street2", "Street 2");

        ComboBoxItem addressType = new ComboBoxItem("addressType", "Address Type");
        addressType.setType("comboBox");
        addressType.setValueMap("Home", "Business", "Mailing", "Priority", "Other");
        addressType.setDefaultToFirstOption(true);

        TextItem street3 = new TextItem("street3", "Street 3");

        RowSpacerItem rowSpacer1 = new RowSpacerItem("rowSpacer1");
        // just want to skip a column, so don't start a new row
        rowSpacer1.setStartRow(false);

        TextItem city = new TextItem("city", "City");

        RowSpacerItem rowSpacer2 = new RowSpacerItem("rowSpacer2");
        // just want to skip a column, so don't start a new row
        rowSpacer2.setStartRow(false);

        TextItem state = new TextItem("state", "State/Province");

        SectionItem addressSection = new SectionItem();
        addressSection.setDefaultValue(ADDRESS_INFORMATION_SECTION_DISPLAY_NAME);
        addressSection.setItemIds("addressName", "postCode", "street1", "country",
                                  "street2", "addressType", "street3", "rowSpacer1", "city", "rowSpacer2",
                                  "state");

        generalForm.setFields(generalSection, accountName, mainPhone, accountNumber,
                              otherPhone, parentAccount, fax, primaryContact, webSite, relationshipType,
                              email, addressSection, addressName, postCode,
                              street1, country, street2, addressType, street3, rowSpacer1, city,
                              rowSpacer2, state);

        generalTab.setPane(generalForm);
    }

    @Override
    public Widget asWidget() {
        return panel;
    }

    public void setServerResponse(AccountDto accountDto) {
        if (accountDto != null) {
            this.accountDto = accountDto;
            setFields(accountDto);
        }
    }

    public void setFields(AccountDto accountDto) {

        accountName.setValue(accountDto.getAccountName());
        accountNumber.setValue(accountDto.getAccountNumber());

    }
}